package com.baosight.sczx.zm.cg.service;

import java.math.BigDecimal;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.Mine;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.cg.domain.ZMCGPictureShow;
import com.baosight.sczx.zm.cg.domain.ZMCGPowerStationRecord;
import com.baosight.sczx.zm.ch.domain.ZMCHProduceReport;
import com.baosight.sczx.zm.cs.domain.ZMCSInitialData;

/**
 * 电厂发电量
 * <p>Title: ServiceZMCG98.java</p> 
 */
public class ServiceZMCG98 extends ServiceEPBase {

	/**
	 * 初始化方法 
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * 查询方法 
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		/*String fdVirtualdate = (String)inInfo.get("fdVirtualdate");
		if(fdVirtualdate != null && !fdVirtualdate.trim().equals("")) {
			StaticTools.setQueryParameters(inInfo, "fdVirtualDate", fdVirtualdate, true);
		}*/
		EiInfo outInfo = super.query(inInfo, "ZMCGPowerStationRecord.query", new ZMCGPowerStationRecord());
		//这里是公司的ID，获得的是矿的列表
		outInfo.addBlock(Mine.getMineBlock("cmes.cg.mine"));
		outInfo.addBlock(UserSession.getUserId().toString());
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cg.powerStation", true));
		return outInfo;
	}


	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p>
	 * <p>return value: ServiceZMCG98</p>
	 * <p>throws Exception:</p>
	 * </p>
	 *
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out = new EiInfo(); 		
		ZMCGPowerStationRecord _dom = new ZMCGPowerStationRecord();
		String now,newDate,fdYear,fdMonth,fdStationname;
//		java.util.Date fdVirtualdate;
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, Object>	val = new HashMap<String, Object>();
			boolean validate = validate(inInfo, _dom, "ZMCGPowerStationRecord.validate1", val);
			if(validate) {
				out = this.query(inInfo);
				return out;
			}else{
			java.util.Date date = new java.util.Date();//当前日期
			java.util.Date fdVirtualdate= _dom.getFdVirtualdate();
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			now = dateFormat.format(date);
			newDate = dateFormat.format(fdVirtualdate);
			fdYear = newDate.substring(0,4);
			fdMonth = newDate.substring(5,7);
			fdStationname = _dom.getFdStationname();
			
			_dom.setFdCreatorId(UserSession.getUserId().toString());
			_dom.setFdYear(Long.parseLong(fdYear));
			_dom.setFdMonth(Long.parseLong(fdMonth));
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CI_POWERSTATIONRECORD"));
			this.dao.insert("ZMCGPowerStationRecord.insert",_dom);
			
//			判断是否是当天记录，如果不是重新计算历史记录的累计值
			if(!newDate.equals(now)){
				fdStationname = _dom.getFdStationname();
				Map<String, Object>	params = new HashMap<String, Object>();
		        params.put("newDate", fdVirtualdate);
		        params.put("fdStationname", fdStationname);
		        List check = this.dao.query("ZMCGPowerStationRecord.check",params);
		        if(check!= null && check.size()>0){
					for(int r=0;r<check.size();r++){
						ZMCGPowerStationRecord bean = (ZMCGPowerStationRecord)check.get(r);
						java.util.Date virtualDate = bean.getFdVirtualdate();
						Long year = bean.getFdYear();
						Long month = bean.getFdMonth();
						String stationName = bean.getFdStationname();
						Map<String, Object>	params1 = new HashMap<String, Object>();
				        params1.put("newDate", virtualDate);
				        params1.put("fdYear", year);
				        params1.put("fdMonth", month);
				        params1.put("fdStationname", stationName);
				        List monthsum = this.dao.query("ZMCGPowerStationRecord.monthsum",params1);
				        if(monthsum != null && monthsum.size() > 0){
				        	HashMap<String,BigDecimal> _tempmonth = (HashMap<String,BigDecimal>)monthsum.get(0);
				        	BigDecimal fdMonthsum = (BigDecimal)_tempmonth.get("FD_MONTHSUM");
				        	BigDecimal fdNetmonthsum = (BigDecimal)_tempmonth.get("FD_NETMONTHSUM");
				        	bean.setFdMonthsum(fdMonthsum);
				        	bean.setFdNetmonthsum(fdNetmonthsum);
				        	this.dao.update("ZMCGPowerStationRecord.update",bean);
						}
				        List yearsum = this.dao.query("ZMCGPowerStationRecord.yearsum",params1);
				        if(yearsum != null && yearsum.size() > 0){
				        	HashMap<String,BigDecimal> _tempyear = (HashMap<String,BigDecimal>)yearsum.get(0);
				        	BigDecimal fdYearsum = (BigDecimal)_tempyear.get("FD_YEARSUM");//发电量年累计
				        	BigDecimal fdNetyearsum = (BigDecimal)_tempyear.get("FD_NETYEARSUM");//上网电量年累计
				        	bean.setFdYearsum(fdYearsum);
				        	bean.setFdNetyearsum(fdNetyearsum);
				        	this.dao.update("ZMCGPowerStationRecord.update",bean);
						}
					}
		        }
		   }
			else{
//			月累计
			Map<String, Object>	params = new HashMap<String, Object>();
	        params.put("newDate", fdVirtualdate);
	        params.put("fdYear", fdYear);
	        params.put("fdMonth", fdMonth);
	        params.put("fdStationname", fdStationname);
	        List monthsum = this.dao.query("ZMCGPowerStationRecord.monthsum",params);
	        if(monthsum != null && monthsum.size() > 0){
	        	HashMap<String,BigDecimal> _temp = (HashMap<String,BigDecimal>)monthsum.get(0);
	        	BigDecimal fdMonthsum = (BigDecimal)_temp.get("FD_MONTHSUM");//发电量月累计
	        	BigDecimal fdNetmonthsum = (BigDecimal)_temp.get("FD_NETMONTHSUM");//上网电量月累计
	        	_dom.setFdMonthsum(fdMonthsum);
	        	_dom.setFdNetmonthsum(fdNetmonthsum);
	        	this.dao.update("ZMCGPowerStationRecord.update",_dom);
			}
	        List yearsum = this.dao.query("ZMCGPowerStationRecord.yearsum",params);
	        if(yearsum != null && yearsum.size() > 0){
	        	HashMap<String,BigDecimal> _temp1 = (HashMap<String,BigDecimal>)yearsum.get(0);
	        	BigDecimal fdYearsum = (BigDecimal)_temp1.get("FD_YEARSUM");//发电量年累计
	        	BigDecimal fdNetyearsum = (BigDecimal)_temp1.get("FD_NETYEARSUM");//上网电量年累计
	        	_dom.setFdYearsum(fdYearsum);
	        	_dom.setFdNetyearsum(fdNetyearsum);
	        	this.dao.update("ZMCGPowerStationRecord.update",_dom);
				}
			  }
			}
		  }
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
	}

	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p>
	 * <p>return value: ServiceZMCG98</p>
	 * <p>throws Exception:</p>
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCGPowerStationRecord _dom = new ZMCGPowerStationRecord();
		String now,newDate,fdYear,fdMonth,fdStationname;
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, Object>	val = new HashMap<String, Object>();
			boolean validate = validate(inInfo, _dom, "ZMCGPowerStationRecord.validate", val);
			if(validate) {
				out = this.query(inInfo);
				return out;
			}else{
			java.util.Date date = new java.util.Date();//当前日期
			java.util.Date fdVirtualdate= _dom.getFdVirtualdate();
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			now = dateFormat.format(date);
			newDate = dateFormat.format(fdVirtualdate);
			fdYear = newDate.substring(0,4);
			fdMonth = newDate.substring(5,7);
			fdStationname = _dom.getFdStationname();

			_dom.setFdYear(Long.parseLong(fdYear));
			_dom.setFdMonth(Long.parseLong(fdMonth));
			_dom.setFdCreatorId(UserSession.getUserId().toString());
			this.dao.update("ZMCGPowerStationRecord.update",_dom);
			
//			判断是否是当天记录，如果不是重新计算历史记录的累计值
			if(!newDate.equals(now)){
				fdStationname = _dom.getFdStationname();
				Map<String, Object>	params = new HashMap<String, Object>();
		        params.put("newDate", fdVirtualdate);
		        params.put("fdStationname", fdStationname);
		        List check = this.dao.query("ZMCGPowerStationRecord.check",params);
		        if(check!= null && check.size()>0){
					for(int r=0;r<check.size();r++){
						ZMCGPowerStationRecord bean = (ZMCGPowerStationRecord)check.get(r);
						java.util.Date virtualDate = bean.getFdVirtualdate();
						Long year = bean.getFdYear();
						Long month = bean.getFdMonth();
						String stationName = bean.getFdStationname();
						Map<String, Object>	params1 = new HashMap<String, Object>();
				        params1.put("newDate", virtualDate);
				        params1.put("fdYear", year);
				        params1.put("fdMonth", month);
				        params1.put("fdStationname", stationName);
				        List monthsum = this.dao.query("ZMCGPowerStationRecord.monthsum",params1);
				        if(monthsum != null && monthsum.size() > 0){
				        	HashMap<String,BigDecimal> _tempmonth = (HashMap<String,BigDecimal>)monthsum.get(0);
				        	BigDecimal fdMonthsum = (BigDecimal)_tempmonth.get("FD_MONTHSUM");
				        	BigDecimal fdNetmonthsum = (BigDecimal)_tempmonth.get("FD_NETMONTHSUM");
				        	bean.setFdMonthsum(fdMonthsum);
				        	bean.setFdNetmonthsum(fdNetmonthsum);
				        	this.dao.update("ZMCGPowerStationRecord.update",bean);
						}
				        List yearsum = this.dao.query("ZMCGPowerStationRecord.yearsum",params1);
				        if(yearsum != null && yearsum.size() > 0){
				        	HashMap<String,BigDecimal> _tempyear = (HashMap<String,BigDecimal>)yearsum.get(0);
				        	BigDecimal fdYearsum = (BigDecimal)_tempyear.get("FD_YEARSUM");//发电量年累计
				        	BigDecimal fdNetyearsum = (BigDecimal)_tempyear.get("FD_NETYEARSUM");//上网电量年累计
				        	bean.setFdYearsum(fdYearsum);
				        	bean.setFdNetyearsum(fdNetyearsum);
				        	this.dao.update("ZMCGPowerStationRecord.update",bean);
						}
					}
		        }
		   }
			else{
//			月累计
			Map<String, Object>	params = new HashMap<String, Object>();
	        params.put("newDate", fdVirtualdate);
	        params.put("fdYear", fdYear);
	        params.put("fdMonth", fdMonth);
	        params.put("fdStationname", fdStationname);
	        List monthsum = this.dao.query("ZMCGPowerStationRecord.monthsum",params);
	        if(monthsum != null && monthsum.size() > 0){
	        	HashMap<String,BigDecimal> _temp = (HashMap<String,BigDecimal>)monthsum.get(0);
	        	BigDecimal fdMonthsum = (BigDecimal)_temp.get("FD_MONTHSUM");
	        	BigDecimal fdNetmonthsum = (BigDecimal)_temp.get("FD_NETMONTHSUM");
	        	_dom.setFdMonthsum(fdMonthsum);
	        	_dom.setFdNetmonthsum(fdNetmonthsum);
	        	this.dao.update("ZMCGPowerStationRecord.update",_dom);
			}
	        List yearsum = this.dao.query("ZMCGPowerStationRecord.yearsum",params);
	        if(yearsum != null && yearsum.size() > 0){
	        	HashMap<String,BigDecimal> _temp1 = (HashMap<String,BigDecimal>)yearsum.get(0);
	        	BigDecimal fdYearsum = (BigDecimal)_temp1.get("FD_YEARSUM");//发电量年累计
	        	BigDecimal fdNetyearsum = (BigDecimal)_temp1.get("FD_NETYEARSUM");//上网电量年累计
	        	_dom.setFdYearsum(fdYearsum);
	        	_dom.setFdNetyearsum(fdNetyearsum);
	        	this.dao.update("ZMCGPowerStationRecord.update",_dom);
				}
			  }
			}
		  }		
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
		
	}	

	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p>
	 * <p>return value: ServiceZMCG98</p>
	 * <p>throws Exception:</p>
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCGPowerStationRecord.delete");
		ZMCGPowerStationRecord _dom = new ZMCGPowerStationRecord();
		String now,newDate,fdStationname;
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			java.util.Date date = new java.util.Date();//当前日期
			java.util.Date fdVirtualdate= _dom.getFdVirtualdate();//需要删除数据的日期
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			now = dateFormat.format(date);
			newDate = dateFormat.format(fdVirtualdate);
//			判断是否是当天记录，如果不是重新计算历史记录的累计值
			if(!newDate.equals(now)){
				fdStationname = _dom.getFdStationname();
				Map<String, Object>	params = new HashMap<String, Object>();
		        params.put("newDate", fdVirtualdate);
		        params.put("fdStationname", fdStationname);
		        List check = this.dao.query("ZMCGPowerStationRecord.check",params);
		        if(check!= null && check.size()>0){
					for(int r=0;r<check.size();r++){
						ZMCGPowerStationRecord bean = (ZMCGPowerStationRecord)check.get(r);
						java.util.Date virtualDate = bean.getFdVirtualdate();
						Long year = bean.getFdYear();
						Long month = bean.getFdMonth();
						String stationName = bean.getFdStationname();
						Map<String, Object>	params1 = new HashMap<String, Object>();
				        params1.put("newDate", virtualDate);
				        params1.put("fdYear", year);
				        params1.put("fdMonth", month);
				        params1.put("fdStationname", stationName);
				        List monthsum = this.dao.query("ZMCGPowerStationRecord.monthsum",params1);
				        if(monthsum != null && monthsum.size() > 0){
				        	HashMap<String,BigDecimal> _tempmonth = (HashMap<String,BigDecimal>)monthsum.get(0);
				        	BigDecimal fdMonthsum = (BigDecimal)_tempmonth.get("FD_MONTHSUM");
				        	BigDecimal fdNetmonthsum = (BigDecimal)_tempmonth.get("FD_NETMONTHSUM");
				        	bean.setFdMonthsum(fdMonthsum);
				        	bean.setFdNetmonthsum(fdNetmonthsum);
				        	this.dao.update("ZMCGPowerStationRecord.update",bean);
						}
				        List yearsum = this.dao.query("ZMCGPowerStationRecord.yearsum",params1);
				        if(yearsum != null && yearsum.size() > 0){
				        	HashMap<String,BigDecimal> _tempyear = (HashMap<String,BigDecimal>)yearsum.get(0);
				        	BigDecimal fdYearsum = (BigDecimal)_tempyear.get("FD_YEARSUM");//发电量年累计
				        	BigDecimal fdNetyearsum = (BigDecimal)_tempyear.get("FD_NETYEARSUM");//上网电量年累计
				        	bean.setFdYearsum(fdYearsum);
				        	bean.setFdNetyearsum(fdNetyearsum);
				        	this.dao.update("ZMCGPowerStationRecord.update",bean);
						}
					}
		        }
		   }
		}
		
//		EiInfo _temp = super.delete(inInfo, "ZMCGPowerStationRecord.delete");
		EiInfo outInfo = this.query(inInfo);
		outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
		
	}	

	/**
	 * <p>Function: report</p>
	 * <p>Description: 生效</p>
	 * <p>return value: ServiceZMCG98</p>
	 * <p>throws Exception:</p>
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo)
	{	
		EiInfo out=new EiInfo(); 
		ZMCGPowerStationRecord _dom = new ZMCGPowerStationRecord();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
	
			this.dao.update("ZMCGPowerStationRecord.report", _dom);
		}	
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	}
	
	/**
	 * 校验每电厂每天只能填报一次
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value: boolean</p>
	 * <p>throws Exception:</p>
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, ZMCGPowerStationRecord _dom, String sqlName, Map<String, Object> params) {
		if(params == null) {
		   params = new HashMap<String, Object>();
		}
		String fdStationname = _dom.getFdStationname();
		String fdKeyId = _dom.getFdKeyId();
		java.util.Date fdVirtualdate= _dom.getFdVirtualdate();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String newDate = dateFormat.format(fdVirtualdate);
		params.put("fdStationname", fdStationname);
		params.put("newDate", fdVirtualdate);
		params.put("fdKeyId", fdKeyId);
		
		List result = dao.query(sqlName, params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			StringBuffer msg = new StringBuffer("提交失败，");
			msg.append("该电厂当日已填报！");
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	
	}
	
	@SuppressWarnings("rawtypes")
	private boolean validate1(EiInfo inInfo, ZMCGPowerStationRecord _dom, String sqlName, Map<String, Object> params) {
		if(params == null) {
		   params = new HashMap<String, Object>();
		}
		String fdStationname = _dom.getFdStationname();
		java.util.Date fdVirtualdate= _dom.getFdVirtualdate();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String newDate = dateFormat.format(fdVirtualdate);
		params.put("fdStationname", fdStationname);
		params.put("newDate", fdVirtualdate);
		
		List result = dao.query(sqlName, params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			StringBuffer msg = new StringBuffer("提交失败，");
			msg.append("该电厂当日已填报！");
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	
	}
}

